package Endereco;

import java.util.List;
import Util.Conexao;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class EnderecoDao {

	Conexao cx = new Conexao();
	
	public void AddEndereco (Endereco end)
	{
		cx.OpenConection();
		cx.getSessao().save(end);
		cx.CloseConection();
	}
	public void RemoveEndereco (Endereco end)
	{
		cx.OpenConection();
		cx.getSessao().delete(end);
		cx.CloseConection();
	}
	public void UpdateEndereco (Endereco end)
	{
		cx.OpenConection();
		cx.getSessao().update(end);
		cx.CloseConection();
	}
	public List <Endereco> ListarEndereco ()
	{
		Criteria listaEnd = cx.getTransacao().createCriteria(Endereco.class);
		listaEnd.addOrder(Order.asc("dscLogradouro"));
		return listaEnd.list();
	}
	public List <Endereco> listarEnderecoDSC (String dsc)
	{
		Criteria listaEnd = cx.getTransacao().createCriteria(Endereco.class);
		listaEnd.add(Restrictions.like("dscLogradouro", "%"+dsc+"%"));
		listaEnd.addOrder(Order.asc("dscLogradouro"));
		return listaEnd.list();
	}
	public Endereco listarEnderecoCOD (int Cod)
	{
		Criteria listaEnd = cx.getTransacao().createCriteria(Endereco.class);
		listaEnd.add(Restrictions.eq("codEndereco", Cod));
		return (Endereco) listaEnd.uniqueResult();
		
	}
	public Endereco listarEnderecoCEP (int CEP)
	{
		Criteria listaEnd = cx.getTransacao().createCriteria(Endereco.class);
		listaEnd.add(Restrictions.eq("CEP", CEP));
		return (Endereco) listaEnd.uniqueResult();
	}
	
}
